package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;

import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.MentorLogger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wwq on 2017/3/16.
 */

public class AssetsFundActivity extends BaseWebViewActivity {

    private String token = "";

    @Override
    protected void onResume() {
        super.onResume();
        if (!token.equals(getToken())) {
            token = getToken();
            generateUrl("assetsFund", HttpUtils.getCommonParams2StringByWeb(mActivity)
                    + "&cusNo=" + getFundCustomerNo());
            if (CommonUtil.isNetWorkConnected(mActivity)) {
                reLoadPage(loadUrl);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
        title.setText("全部资产");
        token = getToken();
        generateUrl("assetsFund", HttpUtils.getCommonParams2StringByWeb(mActivity)
                + "&cusNo=" + getFundCustomerNo());
        if (CommonUtil.isNetWorkConnected(mActivity)) {
            setWebViewConfig(loadUrl);
        }
    }


    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url) {
        MentorLogger.Log(TAG, "url=" + url);
        String url_decode = null;
        try {
            url_decode = URLDecoder.decode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        MentorLogger.Log(TAG, "url_decode=" + url_decode);
        HashMap<String, String> hm = handleUrlParams(url_decode);

        MentorLogger.Log(TAG, "url_decode=" + url_decode);
        String mainTitle;
        Map<String, String> map = new HashMap<>();
        String[] params = url_decode.split("\\?")[1].split("&");
        for (int i = 0; i < params.length; i++) {
            //System.out.println("params--" + params[i]);
            String key = params[i].split("=")[0];
            String value = "";
            try {
                value = params[i].split("=")[1];
            } catch (Exception exception) {
                MentorLogger.Log(TAG, "value为null");
            }
            map.put(key, value);
        }
        mainTitle = map.get("fundName") + " " + map.get("fundCode");

        if (url.contains("prompt.html")) {
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains("assetsFundDetail")) {
            mIntent.setClass(mActivity, AssetsFundDetailActivity.class);
            mIntent.putExtra("openType", "url");
            mIntent.putExtra("fundcode", hm.get("fundCode"));
            mIntent.putExtra("fundname", hm.get("fundName"));
            mIntent.putExtra("fundState", hm.get("fundstateid"));
            mIntent.putExtra("fundstateDescription", hm.get("fundstateDescription"));
            mIntent.putExtra("fundstateid", hm.get("fundstateid"));
            mIntent.putExtra("fundType", hm.get("fundType"));
            mIntent.putExtra("shareClass", hm.get("shareClass"));
            mIntent.putExtra("fundrisklevel", hm.get("fundrisklevel"));
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains("fundBuy")) {
            mIntent.setClass(mActivity, AssetsFundBuyActivity.class);
            String param = url.split("\\?")[1];
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("param", param);
            startActivity(mIntent);
        } else if (url.contains("fundCash")) {
            mIntent.setClass(mActivity, AssetsFundGoCashActivity.class);
            String param = url.split("\\?")[1];
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("param", param);
            startActivity(mIntent);
        } else if (url.contains("transform")) {
            mIntent.setClass(mActivity, AssetsFundTransformActivity.class);
            String param = url.split("\\?")[1];
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("param", param);
            startActivity(mIntent);
        } else if (url.contains("fundInvest.html")) {
            mIntent.setClass(mActivity, AssetsFundInvestActivity.class);
            String param = url.split("\\?")[1];
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("param", param);
            startActivity(mIntent);
        } else if (url.contains("fundInvestAdd.html")) {
            mIntent.setClass(mActivity, AssetsFundInvestAddActivity.class);
            String param = url.split("\\?")[1];
            mIntent.putExtra("mainTitle", mainTitle);
            mIntent.putExtra("param", param);
            startActivity(mIntent);
        } else if (url.contains(WebViewNetworkProtocol.GOTO_LOGIN)) {
            ActivityUtils.gotoLogin(mActivity, hasUser(), false, findUserId());
            EventBus.getDefault().post(new TimeOutEvent(true));
        } else if (url.contains(WebViewNetworkProtocol.GO_QUESTION)) {
            startRiskActivity(null);
        } else {
            webview.loadUrl(url);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

}
